<?php
namespace Admin\Model;
use Think\Model;
class IntentionModel extends Model {
	public $table='zjy_member_address';
	public $page; //当前页
	public $perpage; //每页显示
	public $match_id; //ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类
	public $member_id; //

	//获取意向列表
	public function get_intention_list(){
		$Model=M("Intention");
		$Purchase=M("Purchase");
		$Supply=M("Supply");
		$Member=M("Member");
		$member_id=intval($this->member_id);
		$intention_type=intval($this->intention_type);
		$list=$Model->order('intention_id desc')->select();
		foreach ($list as $k => $v) {
			$purchase_member_id=$Purchase->where('purchase_id='.$v['purchase_id'])->getField('member_id');
			$purchase_member_id=intval($purchase_member_id);
			$list[$k]['purchase_name']=$Member->where('member_id='.$purchase_member_id)->getField('member_truename');
			$supply_member_id=$Supply->where('supply_id='.$v['supply_id'])->getField('member_id');
			$supply_member_id=intval($supply_member_id);
			$list[$k]['supply_name']=$Member->where('member_id='.$supply_member_id)->getField('member_truename');
		}
		return $list;
	}
	//获取意向信息
	public function get_intention_info(){
		$Model=M("Intention");
		$intention_id=intval($this->intention_id);
		$intention_type=intval($this->intention_type);
		$Purchase=M("Purchase");
		$Supply=M("Supply");
		$Member=M("Member");
		$ProductClass=M("ProductClass");
		$Brand=M("Brand");
		$Spec=M("Spec");
		$Unit=M("Unit");
		$info=$Model->where('intention_id='.$intention_id)->find();
		//采购信息
		$info['purchase_info']=$Purchase->where('purchase_id='.$info['purchase_id'])->find();
		$info['purchase_info']['member_truename']=$Member->where('member_id='.intval($info['purchase_info']['member_id']))->getField('member_truename');
		$info['purchase_info']['member_company']=$Member->where('member_id='.intval($info['purchase_info']['member_id']))->getField('member_company');
		$info['purchase_info']['pc_name']=$ProductClass->where('pc_id='.intval($info['purchase_info']['pc_id']))->getField('pc_name');
		$info['purchase_info']['brand_name']=$Brand->where('brand_id='.intval($info['purchase_info']['brand_id']))->getField('brand_name');
		$info['purchase_info']['spec_name']=$Spec->where('spec_id='.intval($info['purchase_info']['spec_id']))->getField('spec_name');
		$info['purchase_info']['unit_name']=$Unit->where('unit_id='.intval($info['purchase_info']['unit_id']))->getField('unit_name');
		//供货信息
		$info['supply_info']=$Supply->where('supply_id='.$info['supply_id'])->find();
		$info['supply_info']['member_truename']=$Member->where('member_id='.intval($info['supply_info']['member_id']))->getField('member_truename');
		$info['supply_info']['member_company']=$Member->where('member_id='.intval($info['supply_info']['member_id']))->getField('member_company');
		$info['supply_info']['pc_name']=$ProductClass->where('pc_id='.intval($info['supply_info']['pc_id']))->getField('pc_name');
		$info['supply_info']['brand_name']=$Brand->where('brand_id='.intval($info['supply_info']['brand_id']))->getField('brand_name');
		$info['supply_info']['spec_name']=$Spec->where('spec_id='.intval($info['supply_info']['spec_id']))->getField('spec_name');
		$info['supply_info']['unit_name']=$Unit->where('unit_id='.intval($info['supply_info']['unit_id']))->getField('unit_name');
		return $info;
	}
	//意向信息添加
	public function intention_add_post(){
		$Model=M("Intention");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//意向编辑提交
	public function intention_edit_post(){
		$Model=M("Intention");
		$post=I('post.');
        $data['intention_message']=$post['intention_message'];
        $data['intention_state']=$post['intention_state'];
		$condition['intention_id'] = $post['intention_id'];
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//批量删除
	public function delete_intention(){
		$ids=$this->ids;
		$Model=M("Intention");
		foreach($ids as $v){
			$condition['intention_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
}